package com.wushijia.datastructure;

import static com.sun.xml.internal.fastinfoset.util.ValueArray.MAXIMUM_CAPACITY;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Test1 {

  public static void main(String args[]) {
    MySkipList.skipListText();

    //HashMap中的key,value都可以为空，但是ConcurrentHashMap中的key,value都不可以为空
    HashMap<String, String> hashMap = new HashMap<>(8);
    System.out.println(tableSizeFor(8));
    hashMap.put(null, null);
    System.out.println(hashMap);

    Map<String, String> concurrentHashMap = new ConcurrentHashMap<>();
//    concurrentHashMap.put(null, null);
//    System.out.println(concurrentHashMap);

    //死循环
    concurrentHashMap.computeIfAbsent("AaAa", key -> {
      return concurrentHashMap.computeIfAbsent("BBBB", key2 -> "42");
    });
    System.out.println(concurrentHashMap);
  }

  static int tableSizeFor(int cap) {
    int n = cap - 1;
    n |= n >>> 1;
    n |= n >>> 2;
    n |= n >>> 4;
    n |= n >>> 8;
    n |= n >>> 16;
    return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
  }
}